#import "JobListUI.h"
#import "PullingRefreshTableView.h"
@interface JobListUI () <
PullingRefreshTableViewDelegate,
UITableViewDataSource,
UITableViewDelegate
>
@property (retain,nonatomic) PullingRefreshTableView *tableView;
@property (retain,nonatomic) NSMutableArray *list;
@property (nonatomic) BOOL refreshing;
@property (assign,nonatomic) NSInteger page;

@end

@implementation JobListUI
@synthesize tableView = _tableView;
@synthesize list = _list;
@synthesize refreshing = _refreshing;
@synthesize page = _page;
@synthesize pageSize,allCnt,job_type;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger row=[indexPath row];
    NSDictionary *dic=[self.list objectAtIndex:row];
    NSString* recruit_id=[dic objectForKey:@"id"];
    JobDetailUI* ui=[[JobDetailUI alloc] init];
    ui.jobId=[recruit_id intValue];
    [self.navigationController pushViewController:ui animated:NO];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 87.0 ;
}

- (void)loadView
{
    [super loadView];
    _list = [[NSMutableArray alloc] init ];
    CGRect frame;
    float iosVersion=[[[UIDevice currentDevice] systemVersion] floatValue];
    
    int h=[Tools getScreenHeight];
    
    
    
    _tableView = [[PullingRefreshTableView alloc] initWithFrame:CGRectMake(0, 44, 320, h-44-65) pullingDelegate:self];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    [self.view addSubview:_tableView];
    
}
-(void)jbFn{
    job_type=1;
    self.refreshing=YES;
    [self.list removeAllObjects];
    [self loadData];
    UIImage* jb=[UIImage imageNamed:@"jb_focus"];
    
    UIImage* ktv=[UIImage imageNamed:@"ktv"];
    
    UIImage* yzh=[UIImage imageNamed:@"yzh"];
    
    NSArray* jbArray=[jbBtn subviews];
    for(UIView* v in jbArray){
        if ([v isKindOfClass:[UILabel class]]) {
            [v removeFromSuperview];
        }
    }
    [jbBtn setBackgroundImage:jb forState:0];
    UILabel* jbLb=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320/3, 44)];
    jbLb.text=@"酒吧";
    jbLb.textAlignment=NSTextAlignmentCenter;
    jbLb.textColor=[UIColor whiteColor];
    jbLb.backgroundColor=[UIColor clearColor];
    [jbBtn addSubview:jbLb];
    /////
    NSArray* ktvArray=[ktvBtn subviews];
    for(UIView* v in ktvArray){
        if ([v isKindOfClass:[UILabel class]]) {
            [v removeFromSuperview];
        }
    }
    [ktvBtn setBackgroundImage:ktv forState:0];
    
    UILabel*  ktvLb=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320/3, 44)];
    ktvLb.text=@"KTV";
    ktvLb.textAlignment=NSTextAlignmentCenter;
    ktvLb.textColor=[UIColor darkGrayColor];
    ktvLb.backgroundColor=[UIColor clearColor];
    [ktvBtn addSubview:ktvLb];
    //
    NSArray* yzhArray=[yzhBtn subviews];
    for(UIView* v in yzhArray){
        if ([v isKindOfClass:[UILabel class]]) {
            [v removeFromSuperview];
        }
    }
    [yzhBtn setBackgroundImage:yzh forState:0];
    UILabel*  yzhLb=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320/3, 44)];
    yzhLb.text=@"会所";
    yzhLb.textAlignment=NSTextAlignmentCenter;
    yzhLb.textColor=[UIColor darkGrayColor];
    yzhLb.backgroundColor=[UIColor clearColor];
    [yzhBtn addSubview:yzhLb];

    
    
    
    
    
}
///////////////
-(void)ktvFn{
    job_type=2;
    self.refreshing=YES;
    [self.list removeAllObjects];
    [self loadData];
    UIImage* jb=[UIImage imageNamed:@"jb"];
    
    UIImage* ktv=[UIImage imageNamed:@"ktv_focus"];
    
    UIImage* yzh=[UIImage imageNamed:@"yzh"];
    
    NSArray* jbArray=[jbBtn subviews];
    for(UIView* v in jbArray){
        if ([v isKindOfClass:[UILabel class]]) {
            [v removeFromSuperview];
        }
    }
    [jbBtn setBackgroundImage:jb forState:0];
    UILabel* jbLb=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320/3, 44)];
    jbLb.text=@"酒吧";
    jbLb.textAlignment=NSTextAlignmentCenter;
    jbLb.textColor=[UIColor darkGrayColor];
    jbLb.backgroundColor=[UIColor clearColor];
    [jbBtn addSubview:jbLb];
    /////
    NSArray* ktvArray=[ktvBtn subviews];
    for(UIView* v in ktvArray){
        if ([v isKindOfClass:[UILabel class]]) {
            [v removeFromSuperview];
        }
    }
    [ktvBtn setBackgroundImage:ktv forState:0];
    
    UILabel*  ktvLb=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320/3, 44)];
    ktvLb.text=@"KTV";
    ktvLb.textAlignment=NSTextAlignmentCenter;
    ktvLb.textColor=[UIColor whiteColor];
    ktvLb.backgroundColor=[UIColor clearColor];
    [ktvBtn addSubview:ktvLb];
    //
    NSArray* yzhArray=[yzhBtn subviews];
    for(UIView* v in yzhArray){
        if ([v isKindOfClass:[UILabel class]]) {
            [v removeFromSuperview];
        }
    }
    [yzhBtn setBackgroundImage:yzh forState:0];
    UILabel*  yzhLb=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320/3, 44)];
    yzhLb.text=@"会所";
    yzhLb.textAlignment=NSTextAlignmentCenter;
    yzhLb.textColor=[UIColor darkGrayColor];
    yzhLb.backgroundColor=[UIColor clearColor];
    [yzhBtn addSubview:yzhLb];
    
    
    
    
    
    
}
-(void)yzhFn{
    job_type=3;
    self.refreshing=YES;
    [self.list removeAllObjects];
    [self loadData];
    UIImage* jb=[UIImage imageNamed:@"jb"];
    
    UIImage* ktv=[UIImage imageNamed:@"ktv"];
    
    UIImage* yzh=[UIImage imageNamed:@"yzh_focus"];
    
    NSArray* jbArray=[jbBtn subviews];
    for(UIView* v in jbArray){
        if ([v isKindOfClass:[UILabel class]]) {
            [v removeFromSuperview];
        }
    }
    [jbBtn setBackgroundImage:jb forState:0];
    UILabel* jbLb=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320/3, 44)];
    jbLb.text=@"酒吧";
    jbLb.textAlignment=NSTextAlignmentCenter;
    jbLb.textColor=[UIColor darkGrayColor];
    jbLb.backgroundColor=[UIColor clearColor];
    [jbBtn addSubview:jbLb];
    /////
    NSArray* ktvArray=[ktvBtn subviews];
    for(UIView* v in ktvArray){
        if ([v isKindOfClass:[UILabel class]]) {
            [v removeFromSuperview];
        }
    }
    [ktvBtn setBackgroundImage:ktv forState:0];
    
    UILabel*  ktvLb=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320/3, 44)];
    ktvLb.text=@"KTV";
    ktvLb.textAlignment=NSTextAlignmentCenter;
    ktvLb.textColor=[UIColor darkGrayColor];
    ktvLb.backgroundColor=[UIColor clearColor];
    [ktvBtn addSubview:ktvLb];
    //
    NSArray* yzhArray=[yzhBtn subviews];
    for(UIView* v in yzhArray){
        if ([v isKindOfClass:[UILabel class]]) {
            [v removeFromSuperview];
        }
    }
    [yzhBtn setBackgroundImage:yzh forState:0];
    UILabel*  yzhLb=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320/3, 44)];
    yzhLb.text=@"会所";
    yzhLb.textAlignment=NSTextAlignmentCenter;
    yzhLb.textColor=[UIColor whiteColor];
    yzhLb.backgroundColor=[UIColor clearColor];
    [yzhBtn addSubview:yzhLb];

}
-(void)publishFn{
    PublishJobUI* ui=[[PublishJobUI alloc] init];
    [self.navigationController pushViewController:ui animated:NO];
}

-(void)reLoadPageFn{
    self.page = 0;
    self.refreshing = YES;
    [self.tableView launchRefreshing];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.pageSize=10;
    job_type=1;
     [[NSNotificationCenter defaultCenter ] addObserver:self selector:@selector(reLoadPageFn) name:@"reLoadPageFn" object:nil];
    self.tableView.contentInset=UIEdgeInsetsMake(0, 0, 110, 0);
    self.view.backgroundColor=[UIColor whiteColor];
    
    UIImage *headImg=[UIImage imageNamed:@"navigation.png"];
    headImg=[Tools resizeImageToSize:headImg toSize:CGSizeMake(320, 44)];
    UINavigationBar *navBar = self.navigationController.navigationBar;
#define kSCNavBarImageTag 10
    if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
    {
        [navBar setBackgroundImage:headImg forBarMetrics:UIBarMetricsDefault];
    }
    else
    {
        UIImageView *imageView = (UIImageView *)[navBar viewWithTag:kSCNavBarImageTag];
        if (imageView == nil)
        {
            imageView = [[UIImageView alloc] initWithImage:
                         headImg];
            [imageView setTag:kSCNavBarImageTag];
            [navBar insertSubview:imageView atIndex:0];
            //[imageView release];
            
        }
    }
    
    
    UILabel* titleLb=[[UILabel alloc] initWithFrame:CGRectMake(120, 0, 80, 40)];
    titleLb.text=@"夜店求职";
    titleLb.font = [UIFont systemFontOfSize:HEAD_SIZE];
    titleLb.textColor=[UIColor whiteColor];
    titleLb.textAlignment=NSTextAlignmentCenter;
    titleLb.backgroundColor=[UIColor clearColor];
    self.navigationItem.titleView=titleLb;
    
    
    
    UIButton* cancelBtn= [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 44)];
    [cancelBtn addTarget:self action:@selector(clickOnBackBut) forControlEvents:UIControlEventTouchUpInside];
    CGRect frame_1= CGRectMake(0,12, 20,20);
    UIImageView*  backView=[[UIImageView alloc] initWithFrame:frame_1];
    UIImage* image= [UIImage imageNamed:@"back2016"];
    backView.image=image;
    [cancelBtn addSubview:backView];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:cancelBtn];
    //
    UIButton* publishBtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0,60, 44)];
    [publishBtn addTarget:self action:@selector(publishFn) forControlEvents:UIControlEventTouchUpInside];
    UILabel* publishLb=[[UILabel alloc] initWithFrame:CGRectMake(0, 7, 60, 30)];
    publishLb.text=@"发布";
    publishLb.textColor=[UIColor whiteColor];
    [publishBtn addSubview:publishLb];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:publishBtn];

    
    
    UIImage* jb=[UIImage imageNamed:@"jb_focus"];
    
    UIImage* ktv=[UIImage imageNamed:@"ktv"];
    
    UIImage* yzh=[UIImage imageNamed:@"yzh"];
    
    jbBtn=[[UIButton alloc] initWithFrame:CGRectMake(1, 0, 320/3, 44)];
    [jbBtn setBackgroundImage:jb forState:0];
    [jbBtn addTarget:self action:@selector(jbFn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:jbBtn];
    UILabel* jbLb=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320/3, 44)];
    jbLb.text=@"酒吧";
    jbLb.textAlignment=NSTextAlignmentCenter;
    jbLb.textColor=[UIColor whiteColor];
    jbLb.backgroundColor=[UIColor clearColor];
    [jbBtn addSubview:jbLb];
    //
    ktvBtn=[[UIButton alloc] initWithFrame:CGRectMake(320/3+1, 0, 320/3, 44)];
    [ktvBtn setBackgroundImage:ktv forState:0];
    [ktvBtn addTarget:self action:@selector(ktvFn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:ktvBtn];
    
    UILabel*  ktvLb=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320/3, 44)];
    ktvLb.text=@"KTV";
    ktvLb.textAlignment=NSTextAlignmentCenter;
    ktvLb.textColor=[UIColor darkGrayColor];
    ktvLb.backgroundColor=[UIColor clearColor];
    [ktvBtn addSubview:ktvLb];
    
    //
    yzhBtn=[[UIButton alloc] initWithFrame:CGRectMake(320*2/3, 0, 320/3, 44)];
    [yzhBtn setBackgroundImage:yzh forState:0];
    [yzhBtn addTarget:self action:@selector(yzhFn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:yzhBtn];
    
    UILabel*  yzhLb=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320/3, 44)];
    yzhLb.text=@"会所";
    yzhLb.textAlignment=NSTextAlignmentCenter;
    yzhLb.textColor=[UIColor darkGrayColor];
    yzhLb.backgroundColor=[UIColor clearColor];
    [yzhBtn addSubview:yzhLb];
    
    
   
    
    
    
    
    [self.tableView setBackgroundView:nil];
    
    if (self.page == 0) {
        [self.tableView launchRefreshing];
    }
}
-(void)clickOnBackBut{
    [self.navigationController popViewControllerAnimated:NO];
}
- (void)viewDidUnload
{
    [super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)loadData{
    self.page++;
    if (self.refreshing) {
        self.page = 0;
        self.refreshing = NO;
        [self.list removeAllObjects];
    }
    
    
    NSString *url=@"";
    url=[HOST stringByAppendingString:@"index.php/AppJobs/findPageList?currentPage="];
    NSMutableString *mUrl=[[NSMutableString alloc] initWithString:url];
    NSString  *sCurrentPage=[NSString stringWithFormat:@"%d",self.page];
    [mUrl appendString:sCurrentPage];
    [mUrl appendString:@"&job_type="];
    
    [mUrl appendString:[NSString stringWithFormat:@"%d",job_type]];
    
    
    
    NSLog(@"murl===%@",mUrl);
    
    
    NSError *error;
    NSString *jsonStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:mUrl] encoding:NSUTF8StringEncoding error:&error];
    NSLog(@"jsonStr===%@",jsonStr);
    
    NSDictionary* tmpDic=[NSDictionary dictionaryWithDictionary:[jsonStr JSONValue]];
    
    NSString* _allCnt=[tmpDic objectForKey:@"size"];
    self.allCnt=[_allCnt intValue];
    NSMutableArray *infoList=[tmpDic objectForKey:@"list"];
    
    for(int i = 0 ; i< [infoList count];i++){
        [self.list addObject: [infoList objectAtIndex:i]];
    }
    [self performSelectorOnMainThread:@selector(didFinishRequest) withObject:nil waitUntilDone:YES];
    
    
    
}
-(void)didFinishRequest{
    NSLog(@"%d====",self.list.count);
    if (self.list.count>=self.allCnt) {
        
         
        
        if (self.allCnt<=self.pageSize) {
            [self.tableView tableViewDidFinishedLoading];
            self.tableView.reachedTheEnd  = YES;
            [self.tableView reloadData];
        }
        if (self.allCnt==self.list.count) {
            [self.tableView tableViewDidFinishedLoading];
            self.tableView.reachedTheEnd  = YES;
            [self.tableView reloadData];
        }
        self.tableView.reachedTheEnd  = YES;
        
    } else {
        [self.tableView tableViewDidFinishedLoading];
        self.tableView.reachedTheEnd  = NO;
        [self.tableView reloadData];
    }
    
}

#pragma mark - TableView*
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.list count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"_CELL";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil){
        cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] ;
    }
    NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
    for (UIView *subview in subviews) {
        if ([subview isKindOfClass:[UILabel class]]) {
            [subview removeFromSuperview];
        }
        if ([subview isKindOfClass:[UIButton class]]) {
            [subview removeFromSuperview];
        }
        if ([subview isKindOfClass:[UIImageView class]]) {
            [subview removeFromSuperview];
        }
        
    }
    
    NSUInteger row=indexPath.row;
    if (row<self.list.count) {
        NSDictionary* dic=[self.list objectAtIndex:row];
        UIImageView* avatarImgView=[[UIImageView alloc] initWithFrame:CGRectMake(4+2,4+2, 68-4, 80-4-4)];
        NSString* avatar=[dic objectForKey:@"pic0"];
        if (![Tools isBlankString:avatar]&&[avatar length]>5) {
            avatar=[@"upload/" stringByAppendingString:avatar];
            
            avatar=[HOST stringByAppendingString:avatar];
            NSLog(@"司机头像=%@",avatar);
            NSURL* avatarURL=[NSURL URLWithString:avatar];
            
            [avatarImgView setImageWithURL:avatarURL placeholderImage:[UIImage imageNamed:@"empty_photo"]];
            [cell.contentView addSubview:avatarImgView];
        }else{
            avatarImgView.image=[UIImage imageNamed:@"empty_photo"];
             [cell.contentView addSubview:avatarImgView];
        }
        
        
        int x=15;
        //标题
        NSString* title=[dic objectForKey:@"reqJobName"];
        
        UILabel* titleLB=[[UILabel alloc] initWithFrame:CGRectMake(68+x/2, 6, 240, 22)];
        
        titleLB.text=title;
        titleLB.font=[UIFont boldSystemFontOfSize:14];
        titleLB.textColor=[UIColor blackColor];
        titleLB.backgroundColor=[UIColor clearColor];
        [cell.contentView addSubview:titleLB];
        
        //////////////////
        NSString* gender=[dic objectForKey:@"gender"];
        int iGender=[gender intValue];
        UILabel* genderLB=[[UILabel alloc] initWithFrame:CGRectMake(68+x/2,28, 120, 22)];
        if (iGender==2) {
            genderLB.text=@"女";
        }else{
            genderLB.text=@"男";
        }
        //
        
        
        
        genderLB.font=[UIFont systemFontOfSize:12];
        genderLB.textColor=[UIColor darkGrayColor];
        genderLB.backgroundColor=[UIColor clearColor];
        [cell.contentView addSubview:genderLB];
        ////////
        NSString* age=[dic objectForKey:@"age"];
        
        UILabel* ageLB=[[UILabel alloc] initWithFrame:CGRectMake(68+x/2,28+22, 120, 22)];
        ageLB.text=[NSString stringWithFormat:@"%@",age];
        
        
        
        ageLB.font=[UIFont systemFontOfSize:12];
        ageLB.textColor=[UIColor darkGrayColor];
        ageLB.backgroundColor=[UIColor clearColor];
        [cell.contentView addSubview:ageLB];
        
        //
        
        NSString* city=[dic objectForKey:@"city"];
        if (city==nil) {
            city=@"上海";
        }

        
        UILabel* cityLB=[[UILabel alloc] initWithFrame:CGRectMake(x+68+100,6, 210, 22)];
        
        cityLB.text=[NSString stringWithFormat:@"%@",city];
        
        cityLB.font=[UIFont systemFontOfSize:12];
        cityLB.textColor=[UIColor grayColor];
        
        
        
        cityLB.backgroundColor=[UIColor clearColor];
        [cell.contentView addSubview:cityLB];
        
        
        ////
        NSString* add_time=[dic objectForKey:@"addTime"];
        UILabel* add_timeLB=[[UILabel alloc] initWithFrame:CGRectMake(x+68+100,28, 210, 22)];
        
        add_timeLB.text=add_time;
        add_timeLB.font=[UIFont systemFontOfSize:12];
        add_timeLB.textColor=[UIColor grayColor];
        
        
        
        add_timeLB.backgroundColor=[UIColor clearColor];
        [cell.contentView addSubview:add_timeLB];
        
        
        
        
        UIImage* arrowImg=[UIImage imageNamed:@"arrow"];
        UIImageView*  arrowImgView=[[UIImageView alloc] initWithFrame:CGRectMake(290, 40, 12*0.9, 21*0.9)];
        arrowImgView.image=arrowImg;
        [cell.contentView addSubview:arrowImgView];
        
        
        //        UIImage* henxian=[UIImage imageNamed:@"凹线条640.png"];
        //        UIImageView *henxianView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
        //        henxianView.image=henxian;
        //        [cell.contentView addSubview:henxianView];
        
        
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

#pragma mark - PullingRefreshTableViewDelegate
- (void)pullingTableViewDidStartRefreshing:(PullingRefreshTableView *)tableView{
    self.refreshing = YES;
    [self performSelector:@selector(loadData) withObject:nil afterDelay:0.1];
}

- (NSDate *)pullingTableViewRefreshingFinishedDate{
    NSDateFormatter *df = [[NSDateFormatter alloc] init ];
    df.dateFormat = @"yyyy-MM-dd HH:mm";
    NSData * date = [NSDate date];//获取当前的时间
    NSString *MyHitOrderListUIStr= [df stringFromDate:date];
    date = [df dateFromString:MyHitOrderListUIStr];
    return date;
}


- (void)pullingTableViewDidStartLoading:(PullingRefreshTableView *)tableView{
    [self performSelector:@selector(loadData) withObject:nil afterDelay:0.1f];
}

#pragma mark - Scroll

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.tableView tableViewDidScroll:scrollView];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self.tableView tableViewDidEndDragging:scrollView];
}

@end
